from django.db import models
from django.contrib.contenttypes import generic

from slots.models import ScheduleMixin, Transition
        
class Style(models.Model):
    name = models.CharField(max_length=64)
    style = models.CharField(max_length=32)
    def __unicode__(self):
        return u'%s - %s' % (self.name, self.style)

class Sponsor(models.Model):
    name = models.CharField(max_length=64)
    product = models.CharField(max_length=64)
    def __unicode__(self):
        return u'%s - %s' % (self.name, self.product)

class Page(ScheduleMixin, models.Model):
    # a page periodically has sponsors and styles, but not always -- so we use
    # schedules and the ScheduleMixin
    name = models.CharField(max_length=64)
    schedules = (Sponsor,Style)
    def __unicode__(self):
        return unicode(self.name)

class Captain(models.Model):
    # standalone model - someone can captain more than one team!
    name = models.CharField(max_length=64)
    def __unicode__(self):
        return self.name

class Team(models.Model):
    # a team plays a sport, and always has a captain - but it changes
    name = models.CharField(max_length=64)
    sport = models.CharField(max_length=64)
    captains = generic.GenericRelation('slots.Transition')
    @property
    def captain(self):
        "The team's current captain"
        # use the manager to get our current Captain
        # current = calculated using datetime.datetime.now()
        # of_type = "I want the object of this type..."
        # for_object = "...related to this instance"
        capt = Transition.objects.current_of_type_for_object(Captain,self)
        if capt is not None:
            return capt.referring_object
        else:
            return None
